package com.qd.panda.refund;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.repeat.annotation.RepeatSubmit;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.common.panda.domain.entity.refund.ParkRefundDTO;
import com.qd.panda.service.refund.ParkRefundService;
import com.qd.panda.service.refund.RefundService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Map;
import java.util.Optional;

/**
 * WEB_退款处理
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/parkRefund/")
@Slf4j
public class ParkRefundController {
    private final ParkRefundService parkRefundService;
    private final RefundService refundService;

    /**
     * 完成退款
     *
     * @param map 订单ID
     * @return true or false
     */
    @RepeatSubmit
    @PreAuthorize("@ps.hasPermission('system:parkRefund:refundComplete')")
    @OperatorInfo(title = "完成退款", operator = OperatorType.OTHER)
    @PostMapping("/refundComplete")
    public ApiResultWrapper<Object> refundComplete(@Validated @RequestBody Map<String, Object> map) {
        Object refundOrderIdObj = map.get("refundOrderId");
        Object refundAmountObj = map.get("refundAmount");
        Object statusObj = map.get("status");
        if (null == refundOrderIdObj) {
            return ApiResult.error("退款单号不能为空");
        }
        if (null == statusObj) {
            return ApiResult.error("状态不能为空");
        }
        int status = (int) statusObj;
        refundService.refundComplete(refundOrderIdObj.toString(), Optional.ofNullable(refundAmountObj).map(Object::toString).orElse(null), status);
        return ApiResult.success();
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public ParkRefundDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return parkRefundService.getByIdDto(id);
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<ParkRefundDTO> page(ParkRefundDTO dto) {
        return parkRefundService.page(PageUtil.buildPage(dto), dto);
    }
}